//
//  MYIndustrialControlViewController.m
//  MiYuan_iOS
//
//  Created by zhangyongbing on 2018/5/11.
//  Copyright © 2018年 zhangyongbing. All rights reserved.
//

#import "MYIndustrialControlViewController.h"
#import "MY_ReplenishmentAPI.h"
#import "IndustrialControlTableCell.h"
#import "MYChangeMachineDetailViewController.h"

@interface MYIndustrialControlViewController ()
@property (nonatomic,copy) NSString *strcontrol;
@property (nonatomic,strong) NSMutableDictionary    *dic_data;
@property (nonatomic,weak) IBOutlet   UILabel     *labNumber;
@property (nonatomic,weak) IBOutlet   UILabel     *labPont;
@property (nonatomic,weak) IBOutlet   UILabel     *labcontrol;
@property (nonatomic,weak) IBOutlet   UILabel     *labAdmin;
@property (nonatomic,weak) IBOutlet   UILabel     *labphone;
@property (nonatomic,weak) IBOutlet   UILabel     *labMoney;
@property (nonatomic,weak) IBOutlet   UIView          *viewHead;

@end

@implementation MYIndustrialControlViewController

-(instancetype)initWithControlNumber:(NSString *)number
{
    self = [super initWithNibName:@"MYIndustrialControlViewController" bundle:nil];
    if (self) {
        self.strcontrol = number;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    NSString *version= [UIDevice currentDevice].systemVersion;
    
    if(version.doubleValue >=11.0) {
        
    }else{
        // 针对 11.0 以下的iOS系统进行处理
        for (NSLayoutConstraint *constraint in self.viewHead.superview.constraints) {
            if (constraint.firstItem == self.viewHead && constraint.firstAttribute == NSLayoutAttributeTop) {
                constraint.constant = 20;
            }
        }
        
    }
    [self getDataFromServices];
}

- (void)getDataFromServices
{
    [MY_ReplenishmentAPI my_GetReplenishmentWithControlnumber:self.strcontrol complationHandler:^(OCJBaseResponceModel *responseModel) {
        [self.dic_data removeAllObjects];
        [self.dic_data addEntriesFromDictionary:responseModel.ocjDic_data];
        [self reloadDetailDataToview];
    }];
}

- (void)reloadDetailDataToview
{
    NSString *number = [NSString stringWithFormat:@"%@",[self.dic_data objectForKey:@"number"]];
    if (![number stringIsValid]) {
        number = @"";
    }
    [self.labNumber setText:number];
    NSString *pointName = [NSString stringWithFormat:@"%@",[self.dic_data objectForKey:@"pointName"]];
    
    if (![pointName stringIsValid]) {
        pointName = @"";
    }
    [self.labPont setText:pointName];
    NSString *controlNumber = [NSString stringWithFormat:@"%@",[self.dic_data objectForKey:@"controlNumber"]];
    if (![controlNumber stringIsValid]) {
        controlNumber = @"";
    }
    [self.labcontrol setText:controlNumber];
    NSString *name = [NSString stringWithFormat:@"%@",[self.dic_data objectForKey:@"name"]];
    if (![name stringIsValid]) {
        name = @"";
    }
    [self.labAdmin setText:name];
    NSString *phone = [NSString stringWithFormat:@"%@",[self.dic_data objectForKey:@"phone"]];
    if (![phone stringIsValid]) {
        phone = @"";
    }
    [self.labphone setText:phone];
    NSString *totalAmount = [NSString stringWithFormat:@"%@",[self.dic_data objectForKey:@"totalAmount"]];
    if (![totalAmount stringIsValid]) {
        totalAmount = @"";
    }
    [self.labMoney setText:totalAmount];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)onBackButtonPressed:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)onChangeErrorButtonPressed:(id)sender
{
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    [dic setObject:self.labNumber.text forKey:@"number"];
    [dic setObject:self.labcontrol.text forKey:@"controlNumber"];
    [dic setObject:self.labPont.text forKey:@"pointName"];
    
    MYChangeMachineDetailViewController *ctrl = [[MYChangeMachineDetailViewController alloc] initWithMachineDetailDic:dic];
    [self.navigationController pushViewController:ctrl animated:YES];
    
}

- (IBAction)onSureButtonPressed:(id)sender
{
    NSString *totalAmount = [NSString stringWithFormat:@"%@",[self.dic_data objectForKey:@"totalAmount"]];
    NSString *struserid = [NSString stringWithFormat:@"%@",[[PublicDefine shareInstance].dicUserInfo objectForKey:@"id"]];
    NSString *vendorId = [NSString stringWithFormat:@"%@",[self.dic_data objectForKey:@"vendorId"]];
    NSMutableDictionary *dicpost = [[NSMutableDictionary alloc] init];
    [dicpost setObject:self.strcontrol forKey:@"controlNumber"];
    [dicpost setObject:totalAmount forKey:@"totalAmount"];
    [dicpost setObject:struserid forKey:@"userId"];
    [dicpost setObject:vendorId forKey:@"vendorId"];
    
    [MY_ReplenishmentAPI my_AddReplenishmentWithPostData:dicpost complationHandler:^(OCJBaseResponceModel *responseModel) {
        if ([responseModel.ocjStr_code isEqualToString:@"0"]) {
            
            UIAlertView *alertDialog = [[UIAlertView alloc] initWithTitle:nil message:@"补货成功！" delegate: self cancelButtonTitle:@"确定" otherButtonTitles: nil];
            
            
            alertDialog.tag = 10001;
            [alertDialog show];
        }
        
    }];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 10001) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}


- (NSMutableDictionary *)dic_data
{
    if (_dic_data == nil) {
        _dic_data = [[NSMutableDictionary alloc] init];
    }
    return _dic_data;
}

@end
